﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddType : ATPages
{
    ZSql zsql = new ZSql();
    Function f = new Function();
    private void initType1()
    {
        ZSql type = new ZSql();
        type.Open("select * from ComoditiesType ");
        if (type.m_table.Rows.Count > 0)
        {
            type.Open("select * from ComoditiesType where AuditState=1");
            if (type.m_table.Rows.Count > 0)
            {
                ddlType.DataSource = type.m_table;
                ddlType.DataTextField = "ComoditiesType";
                ddlType.DataValueField = "TypeId";
                ddlType.DataBind();
                
                type.Close();
            }
            else
            {
                Response.Write("<script>alert('商品类型未审核');</script>");
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
        }
        else
        {
            Response.Write("<script>alert('商品类型为空请添加类型');</script>");
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
        ddlType.Items.Insert(0, new ListItem("—选择商品类型—", "0"));
      
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidID1.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            if (Request.QueryString["action"].ToString() == "update")
            {
                hidId.Value = Request.QueryString["BrandId"].ToString();
            
                btnSave.Text = " 修改 ";
                ZSql zs = new ZSql();
                zs.Open("select * from Brand where BrandId=" + hidId.Value.Trim());
                ZSql t = new ZSql();
                t.Open("select * from ComoditiesType where AuditState=1 ");
                ddlType.DataSource = t.m_table;
                ddlType.DataTextField = "ComoditiesType";
                ddlType.DataValueField = "TypeId";
                ddlType.DataBind();
                t.Close();
                ddlType.Items.Insert(0, new ListItem("—选择商品类型—","0"));
                ddlType.SelectedValue = zs.m_table.Rows[0]["TypeId"].ToString();
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);

            }
            if (Request.QueryString["action"].ToString() == "add")
            {
            
                btnSave.Text = " 添加 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                initType1();
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                hidId.Value = Request.QueryString["BrandId"].ToString();
        
                btnSave.Text = " 审核 ";
                ZSql zs = new ZSql();
                zs.Open("select * from Brand where BrandId=" + hidId.Value.Trim() + "");
                ZSql t = new ZSql();
                t.Open("select * from ComoditiesType where AuditState=1 ");
                ddlType.DataSource = t.m_table;
                ddlType.DataTextField = "ComoditiesType";
                ddlType.DataValueField = "TypeId";
                ddlType.DataBind();
                t.Close();
                ddlType.Items.Insert(0, new ListItem("—选择商品类型—","0"));
                ddlType.SelectedValue = zs.m_table.Rows[0]["TypeId"].ToString();
                if (zs.m_table.Rows[0]["AuditState"].ToString() == "1")
                {
                    rbtnFlag.Checked = true;
                    rbtnnotFlag.Checked = false;
                }
                if (zs.m_table.Rows[0]["AuditState"].ToString() == "0")
                {
                    rbtnFlag.Checked = false;
                    rbtnnotFlag.Checked = true;
                }
                
            }
            
           
            initType();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ZSql zs = new ZSql();
            if (!string.IsNullOrEmpty(hidId.Value))
            {

                int flag;
                if (rbtnFlag.Checked)
                {
                    flag = 1;
                }
                else
                {
                    flag = 0;
                }

                if (Request.QueryString["action"].ToString() == "flag")
                {
                    zs.Open("update Brand set UserName='" + lblUser.Text + "',usTime='" + lblTime.Text + "',AuditState='" + flag + "',brandbh='"+Common.FormatParameter(txtbh.Text)+"' where BrandId=" + hidId.Value);
                    Response.Write("<script>window.location='BrandManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
                else
                {
                    //zs.Open("select b.TypeId,Brand,c.TypeId from Brand as b,ComoditiesType as c where Brand='" + Common.FormatParameter(txtBrand.Text) + "' and b.TypeId='"+ddlType.SelectedValue+"'");
                    //if (zs.m_table.Rows.Count > 0)
                    //{
                    //    Response.Write("<script>alert('该品牌已存在');location.href=document.referrer</script>");
                    //    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                    //    return;
                    //}
                    zs.Open("update Brand set Brand='" + Common.FormatParameter(txtBrand.Text) + "',BrandOrder='" + Common.FormatParameter(txtOrder.Text) + "',BrandDescription='" + Common.FormatParameter(txtDescription.Text) + "',TypeId='" + ddlType.SelectedValue + "',UserName='" + lblUser.Text + "',usTime='" + lblTime.Text + "',brandbh='"+Common.FormatParameter(txtbh.Text)+"' where BrandId=" + hidId.Value);

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                    Response.Write("<script>window.location='BrandManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }


            }
            else
            {
                if (ddlType.SelectedValue == "0")
                {
                     Response.Write("<script>alert('请选择商品类型');location.href=document.referrer</script>");
                }
                if(ddlType.SelectedValue != "0")
                {
                    zs.Open("select b.TypeId,Brand,c.TypeId from Brand as b,ComoditiesType as c where Brand='" + Common.FormatParameter(txtBrand.Text) + "' and b.TypeId='" + ddlType.SelectedValue + "'");
                    if (zs.m_table.Rows.Count > 0)
                    {
                        Response.Write("<script>alert('该品牌已存在');location.href=document.referrer</script>");
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                        return;
                    }
                    zs.Open("insert into Brand (Brand,BrandOrder,BrandDescription,TypeId,UserName,usTime,brandbh) values('" + Common.FormatParameter(txtBrand.Text) + "','" + Common.FormatParameter(txtOrder.Text) + "','" + Common.FormatParameter(txtDescription.Text) + "','" + ddlType.SelectedValue + "','" + lblUser.Text + "','" + lblTime.Text + "','"+Common.FormatParameter(txtbh.Text)+"')");


                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                    Response.Write("<script>window.location='BrandManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
                zs.Close();
            }
           
        }
        catch (Exception)
        {

            Response.Write("<script>alert('添加失败');location.href=document.referrer</script>");
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
    }

    private void initType()
    {
        ZSql type = new ZSql();
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            type.Open("select Brand,BrandOrder,BrandDescription,brandbh from Brand where BrandId=" + hidId.Value);
            txtBrand.Text = type.m_table.Rows[0]["Brand"].ToString();
            txtOrder.Text = type.m_table.Rows[0]["BrandOrder"].ToString();
            txtDescription.Text = type.m_table.Rows[0]["BrandDescription"].ToString();
            txtbh.Text = type.m_table.Rows[0]["brandbh"].ToString();
            type.Close();
        }
    }
    protected void btnSelec_Click(object sender, EventArgs e)
    {
        //zsql.Open("select * from Brand where Brand like'%" + txtselecName.Text + "%'");
        //GvData1.DataSource = zsql.m_table;
        //GvData1.DataBind();
        //flag();
    }
}
